import {terrariaMap} from "../terrariaMap.js";
import {mainController} from "../mainController.js";
import {elements} from "../elements.js";

const {canvas} = elements
const getMousePosition = (event)=>{
  let rectangle = canvas.getBoundingClientRect();
  let mouseX = (event.clientX - rectangle.left);
  let mouseY = (event.clientY - rectangle.top);
  return {x: mouseX, y: mouseY}
}


export function mouseCreatEventHandler(event) {
  let pos = getMousePosition(event);
  pos.y = Math.floor(pos.y / 10) * 10
  pos.x = Math.floor(pos.x / 10) * 10
  terrariaMap.creat(pos);
}

export function mouseDeleteEventHandler(event) {
  let pos = getMousePosition(event);
  pos.y = Math.floor(pos.y / 10) * 10
  pos.x = Math.floor(pos.x / 10) * 10
  terrariaMap.delete(pos);
}

/**
 *
 * @param event {KeyboardEvent}
 */
export function keyFlagChangeTo1(event) {
  let code = event.keyCode
  if (code === 65 || code === 37) {
    mainController.getHero().parallelMove(-1)
  }else if(code === 68 || code === 39) {
    mainController.getHero().parallelMove(1)
  }else if (code === 32) {
    mainController.getHero().jump()
  }
}

/**
 *
 * @param event {KeyboardEvent}
 */
export function keyFlagChangeTo0(event) {
  let code = event.keyCode
  if (code === 65 || code === 37) {
    mainController.getHero().cancelParallelMove(-1)
  }else if(code === 68 || code === 39) {
    mainController.getHero().cancelParallelMove(1)
  }else if (code === 32) {
    mainController.getHero().cancelJump()
  }
}